coreExamPaper.startPage = {

    getGridParam: function () {
        let _this = this;
        return {
            type: 'grid',
            id: 'core-exam-start-list-grid',
            url: coreExamPaper.baseUrl + '/queries/raw',
            requestData: {
                status_EQ: 'draft'
            },
            deleteFormatter: function (row) {
                return row.ext$.examname
            },
            page: true,
            toolbarAlign: 'left',
            columns: coreExamPaper.getStartListColumns(),
            toolbar: [{
                type: 'button',
                text: 'CORE.MODULE.TRAIN.TRAIN.BUTTON.ADD',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'CORE.MODULE.TRAIN.TRAIN.BUTTON.ADD',
                        fields: coreExamPaper.getStartFormFields(),
                        url: coreExamPaper.baseUrl,
                        height: 380,
                        width: 500,
                        disableNotEmptyValidate: true
                    }).done(function (id) {
                        Gikam.getComp('core-exam-start-list-grid').refresh();
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove',
                onClick: function () {
                    Gikam.getComp('core-exam-start-list-grid').deleteRows(coreExamPaper.baseUrl);
                }
            }, {
                type: 'button',
                text: 'T_CORE_EXAM_PAPER_EXT.CHOOSE.USERNAME',
                icon: 'edit',
                onClick: function () {
                    let _this = this;
                    let grid = Gikam.getComp('core-exam-start-list-grid');
                    let selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_ONE_ITEM');
                        return;
                    }
                    let userIds = "";
                    if (selections.length === 1) {
                        userIds = selections[0].examUserId;
                    }
                    Gikam.create('modal', {
                        title: Gikam.choose.getConfig('mult-user').title,
                        url: IFM_CONTEXT + Gikam.choose.getConfig("mult-user").url + Gikam.param({
                            userIds: userIds,
                            status_EQ: 'activated',
                        }),
                        onAfterClose: function (rows) {
                            if (Gikam.isEmpty(rows)) {
                                return;
                            }
                            let personid = '', personname = '';
                            let rowlength = rows.length - 1;
                            Gikam.each(rows, function (index, item) {
                                if (index == rowlength) {
                                    personid += item.id;
                                    personname += item.userName;
                                } else {
                                    personid += item.id + ',';
                                    personname += item.userName + ',';
                                }
                            });
                            selections.forEach(s => {
                                s.examUserId = personid;
                                s.examUserName = personname
                            });
                            workspace.window.showMask();
                            Gikam.postText(coreExamPaper.baseUrl + '/action/update-user', Gikam.getJsonWrapper({}, [null, selections])).done(function () {
                                Gikam.alert('BASEDATA.MODULE.PRODUCT.CATEGORY.UPDATE.SUCCESS');
                                grid.refresh();
                            }).always(function () {
                                workspace.window.closeMask();
                            });
                        }
                    });
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.EDIT_EXAM_TIME',
                icon: 'edit',
                onClick: function () {
                    var grid = Gikam.getComp('core-exam-start-list-grid')
                    let selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.createLimsModal({
                        title: 'GIKAM.BUTTON.EDIT',
                        width: 500,
                        height: 250,
                        onConfirm: function () {
                            var form = Gikam.getComp('edit-exam-time-form');
                            if (form.validate()) {
                                var data = form.getData()
                                var datas = selections.map(e => {
                                    return {
                                        id: e.id,
                                        examBeginTime: data.examBeginTime,
                                        examEndTime: data.examEndTime,
                                        examDuration: data.examDuration
                                    }
                                })
                                Gikam.put(coreExamPaper.baseUrl + '/instant', Gikam.getJsonWrapper(null, [null, datas])).done(function () {
                                    grid.refresh()
                                })
                                Gikam.getLastModal().close(data)
                            }
                        },
                        m: {
                            type: 'form',
                            id: 'edit-exam-time-form',
                            columns: 1,
                            titleWidth: 100,
                            onRendered: function () {
                                this.setData({
                                    examBeginTime: selections[0].examBeginTime,
                                    examEndTime: selections[0].examEndTime
                                });
                            },
                            fields: [{
                                field: 'examBeginTime',
                                title: 'T_CORE_EXAM.EXAMBEGINTIME',
                                type: 'dateTime',
                                validators: ['notEmpty'],
                                onChange: function (field, value, index) {
                                    if (Gikam.isNotEmpty(this.options.data.examEndTime) && value) {
                                        let endDate = new Date(this.options.data.examEndTime);
                                        let startDate = new Date(value);
                                        if (startDate > endDate) {
                                            this.setData({
                                                examBeginTime: '',
                                                examDuration: '',
                                                id: this.options.data.id
                                            });
                                            Gikam.alert('STARTDATE.NO.MORE.THAN.ENDDATE');
                                        } else {
                                            let ss = endDate.getTime() - startDate.getTime();
                                            let min = Math.floor(ss / 60000);
                                            this.setData({
                                                examBeginTime: value,
                                                examDuration: min,
                                                id: this.options.data.id
                                            });
                                            this.setData({
                                                examBeginTime: value,
                                                id: this.options.data.id
                                            });
                                        }
                                    }
                                },
                                preInsert: true
                            }, {
                                field: 'examEndTime',
                                title: 'T_CORE_EXAM.EXAMENDTIME',
                                type: 'dateTime',
                                validators: ['notEmpty'],
                                onChange: function (field, value, index) {
                                    if (Gikam.isNotEmpty(this.options.data.examBeginTime) && value) {
                                        let startDate = new Date(this.options.data.examBeginTime);
                                        let endDate = new Date(value);
                                        if (startDate > endDate) {
                                            this.setData({
                                                examEndTime: '',
                                                examDuration: '',
                                                id: this.options.data.id
                                            });
                                            Gikam.alert('STARTDATE.NO.MORE.THAN.ENDDATE');
                                        } else {
                                            let ss = endDate.getTime() - startDate.getTime();
                                            let min = Math.floor(ss / 60000);
                                            this.setData({
                                                examEndTime: value,
                                                examDuration: min,
                                                id: this.options.data.id
                                            });
                                        }
                                    }
                                },
                                preInsert: true
                            },],
                        },
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.MODAL.SUBMIT',
                icon: 'submit',
                color: 'success',
                onClick: function () {
                    let grid = Gikam.getComp('core-exam-start-list-grid');
                    let selections = grid.getSelections();
                    if (Gikam.isEmpty(selections)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.confirm('GIKAM.BUTTON.CONFIRM', 'GIKAM.TIP.SUBMIT', function () {
                        workspace.window.showMask();
                        Gikam.postText(coreExamPaper.baseUrl + '/submit', Gikam.getJsonWrapper(null, [null, selections]))
                            .done(function () {
                                grid.refresh();
                            }).always(function () {
                            workspace.window.closeMask();
                        })
                    })
                }
            }]
        }
    },

    getDoneGridParam: function () {
        let _this = this;
        return {
            type: 'grid',
            id: 'core-exam-started-already-list-grid',
            url: coreExamPaper.baseUrl + '/queries/raw',
            requestData: {
                statusDone: '1'
            },
            toolbarAlign: 'left',
            columns: coreExamPaper.getStartDoneListColumns(),
            toolbar: [],
            page: true,
        }
    },

    getMainTabs: function () {
        var _this = this
        var first = false
        return {
            type: 'tab',
            id: 'main-tab',
            panels: [{
                title: 'BASEMODULE.RM.TRAINS.TAB.UN_DONE',
                items: [_this.getGridParam()]
            }, {
                title: 'BASEMODULE.SAMPLEMANAGE.SAMPLESTAGINGS.TAB.SOLVED',
                items: [_this.getDoneGridParam()]
            }],
            onActived: function (data) {
                switch (data.index) {
                    case 0:
                        if (first) {  // 首次加载页面时，不操作
                            Gikam.getAsyncComp('core-exam-start-list-grid').done(function (grid) {
                                grid.refresh()
                            })
                        }
                        break
                    case 1:
                        Gikam.getAsyncComp('core-exam-started-already-list-grid').done(function (grid) {
                            grid.refresh({
                                url: coreExamPaper.baseUrl + '/search-exampapers',
                            })
                        })
                        break
                }
                first = true
            }
        }
    },

    create: function () {
        let _this = this;
        Gikam.create('layout', {
            id: 'core-exam-start-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [_this.getMainTabs()]
            }
        })
    },

    init: function () {
        this.create();
    }
};